import define.Autowrite;
import define.Controller;
import define.Mapper;
import define.Param;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        disposeController();
        //disposeAutowrite();
        //disposeMapper();
        //disposeParam();
    }

    private static void disposeController() {
        try {
            Class cla = Class.forName("TestController");
            if (cla.isAnnotationPresent(Controller.class)) {
                System.out.println("TestController类含有Controller注解");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void disposeAutowrite() {
        try {
            Class cla = Class.forName("TestController");
            Field[] fields = cla.getDeclaredFields();
            Arrays.stream(fields).filter((v) -> v.isAnnotationPresent(Autowrite.class)).forEach((v) -> {
                System.out.println(v.getName() + "字段有Autowrite注释");
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void disposeMapper() {
        try {
            Class cla = Class.forName("TestController");
            Method[] methods = cla.getDeclaredMethods();
            Arrays.stream(methods).filter((v) -> v.isAnnotationPresent(Mapper.class)).forEach((v) -> {
                System.out.println(v.getName() + "方法有Mapper注释");
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void disposeParam() {
        try {
            Class cla = Class.forName("TestController");
            Method[] methods = cla.getDeclaredMethods();
            Arrays.stream(methods).forEach((v) -> {
                Parameter[] parameters = v.getParameters();
                Arrays.stream(parameters).filter((a)->a.isAnnotationPresent(Param.class)).forEach((aa)->{
                    Annotation annotation = aa.getAnnotation(Param.class);
                    System.out.println(v.getName() + "方法的" + aa.getName() + "参数有Param注解 值是" + ((Param) annotation).value());
                    //注明一点 aa.getName() 所得到的名称实际上是arg0、arg1一类的 原因是编译器在编译的时候将参数名称替换成了arg+index格式
                });
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
